package com.eleven.mall.controller;

import com.eleven.mall.pojo.CategoryOut;
import com.eleven.mall.pojo.CatesPage;
import com.eleven.mall.pojo.OrderPage;
import com.eleven.mall.pojo.Rbody;
import com.eleven.mall.service.CategoryService;
import com.eleven.mall.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@AllArgsConstructor
@CrossOrigin
public class CategoryController {

//    分类列表
    CategoryService categoryService;

    @GetMapping("/categories")
    public Rbody getCatList(Integer type,Integer pagenum,Integer pagesize){
        System.out.println("==========查询参数:"+type);
        Rbody rbody = new Rbody();

        List<CategoryOut> categoryOut = categoryService.getCatList(type);

        if (pagenum!=null && pagesize!=null){
            PageHelper.startPage(pagenum,pagesize);
            PageInfo<CategoryOut> catesListInfo = new PageInfo<>(categoryOut);

            CatesPage catesPage =  new CatesPage();
            catesPage.setTotal(catesListInfo.getTotal());
            catesPage.setPagenum(catesListInfo.getPageNum());
            catesPage.setPagesize(catesListInfo.getPageSize());
            catesPage.setResult(categoryOut);
            rbody.setData(catesPage);
        }else {
            rbody.setData(categoryOut);
        }
        rbody.getMeta().setMsg("获取成功");
        return rbody;
    }
}
